// 设备交互的命令封装
package device

import (
	"context"
	"device-admin/config/redis"
	"device-admin/internal/app/grpc/protoc/devicec"
	"device-admin/internal/app/web/service/cmd"
	"device-admin/internal/define"
	"time"

	"192.168.1.75/go-pkg/errorx"
	"192.168.1.75/go-pkg/helper"
	"google.golang.org/protobuf/proto"
)

// DoorCtlCMDPack 电子门锁控制
func DoorCtlCMDPack(robotID string, doorType int, status int) error {
	if robotID == "" {
		return errorx.New("设备id不能为空")
	}
	cmd := &devicec.DoorCtlCMD{}
	cmd.DoorType = int32(doorType)
	if status == 1 {
		cmd.Status = true
	}
	name := string(cmd.ProtoReflect().Descriptor().FullName())
	content, err := proto.Marshal(cmd)
	MsgPack(robotID, name, content, "", "97")
	return err
}

// LiftPostionCMDPack 设置升降盘梯位置
func LiftPostionCMDPack(robotID string, floor int) error {
	if robotID == "" {
		return errorx.New("设备id不能为空", -1)
	}
	cmd := &devicec.LiftPostion{}
	cmd.Floor = int32(floor)
	name := string(cmd.ProtoReflect().Descriptor().FullName())
	content, err := proto.Marshal(cmd)
	MsgPack(robotID, name, content, "", "97")
	return err
}

// SaleStatusCMDPack 获取售卖状态
func SaleStatusCMDPack(robotID string) (seq string, status int64, err error) {
	if robotID == "" {
		return seq, -1, errorx.New("设备id不能为空", -1)
	}
	saleStatusReq := &devicec.SaleStatusReq{}
	name := string(saleStatusReq.ProtoReflect().Descriptor().FullName())
	content, _ := proto.Marshal(saleStatusReq)
	seq = "waitmsg_" + helper.RandString(16)
	_, err = MsgPack(robotID, name, content, seq, "97")
	if err != nil {
		return seq, -1, err
	}
	// 等待执行的结果
	resData, err := cmd.WaitCmdRsp(robotID, "msg", seq, "97")
	if err != nil {
		return seq, -1, err
	}
	saleStatusRsp := &devicec.SaleStatusRsp{}
	proto.Unmarshal(resData, saleStatusRsp)
	return seq, saleStatusRsp.Status, nil
}

// StopSaleCMDPack 停止售卖
func StopSaleCMDPack(robotID string) (seq string, err error) {
	if robotID == "" {
		return "", errorx.New("设备id不能为空", -1)
	}
	stopSaleReq := &devicec.StopSaleReq{}
	name := string(stopSaleReq.ProtoReflect().Descriptor().FullName())
	content, _ := proto.Marshal(stopSaleReq)
	seq = "waitmsg_" + helper.RandString(16)
	_, err = MsgPack(robotID, name, content, seq, "97")
	if err != nil {
		return seq, err
	}
	// 等待执行的结果
	resData, err := cmd.WaitCmdRsp(robotID, "msg", seq, "97")
	if err != nil {
		return seq, err
	}
	stopSaleRsp := &devicec.StopSaleRsp{}
	proto.Unmarshal(resData, stopSaleRsp)
	if stopSaleRsp.Code != 0 {
		return seq, errorx.New("停止售卖失败", -1)
	}
	return seq, nil
}

// ResumeSaleCMDPack 恢复售卖
func ResumeSaleCMDPack(robotID string) (seq string, err error) {
	if robotID == "" {
		return seq, errorx.New("设备id不能为空", -1)
	}
	resumeSaleReq := &devicec.ResumeSaleReq{}
	name := string(resumeSaleReq.ProtoReflect().Descriptor().FullName())
	content, _ := proto.Marshal(resumeSaleReq)
	seq = "waitmsg_" + helper.RandString(16)
	_, err = MsgPack(robotID, name, content, seq, "97")
	if err != nil {
		return seq, err
	}
	// 等待执行的结果
	resData, err := cmd.WaitCmdRsp(robotID, "msg", seq, "97")
	if err != nil {
		return seq, err
	}
	resumeSaleRsp := &devicec.ResumeSaleRsp{}
	proto.Unmarshal(resData, resumeSaleRsp)
	if resumeSaleRsp.Code != 0 {
		return seq, errorx.New("恢复售卖失败", -1)
	}
	return seq, nil
}

// RebootSaleCMDPack 重启97
func RebootSaleCMDPack(robotID string) (seq string, err error) {
	if robotID == "" {
		return seq, errorx.New("设备id不能为空", -1)
	}
	reboot := &devicec.Reboot{}
	name := string(reboot.ProtoReflect().Descriptor().FullName())
	content, _ := proto.Marshal(reboot)
	seq = "waitmsg_" + helper.RandString(16)
	_, err = MsgPack(robotID, name, content, seq, "97")
	if err != nil {
		return seq, err
	}
	return seq, nil
}

// SaveSaleStatus 保存售卖状态
func SaveSaleStatus(robotID string, status int) {
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
	defer cancel()
	redis.NewDB().HSet(ctx, define.RedisRobotRTInfo+robotID, "sale_status", status)
}
